import re
from selenium.webdriver.common.keys import Keys

from qturboframe.lib.webdriver.exceptions import SeleniumError
from qturboframe.public_ui.base_element import BaseElement


class Page(BaseElement):
    '''hui表格的分页栏'''

    def __init__(self, element_para, father=None, timeout=5):
        try:
            super().__init__(element_para, father=father, timeout=timeout)
        except SeleniumError as msg:
            self._element = None

    def exist(self):
        '''
        分页栏是否存在

        :Return:
            分页栏存在，则返回True，否则返回False

        :Example:
         - 例: page.exist()
        '''
        if self._element:
            return True
        else:
            return False

    def to_first(self):
        '''
        翻至表格首页

        :Example:
         - 例: page.to_first()
        '''
        if self.is_first():
            page_first = self._element.exist(
                'css selector', 'li.h-page-item>a', 'text:1', timeout=0.1)
            if page_first:
                page_first.secure_click()
            else:
                page_first_input = self._element.find_element_by_attribute(
                    'css selector', 'div.h-page-simple-pager>input', timeout=0.1)
                page_first_input.clear()
                page_first_input.send_keys('1')
                page_first_input.send_keys(Keys.ENTER)

    def to_next(self):
        '''
        翻至表格下一页

        :Example:
         - 例: page.to_next()
        '''
        page_next = self._element.find_element_by_attribute(
            'css selector', 'li.h-page-next[title="下一页"]>a', timeout=self.timeout)
        page_next.secure_click()

    def is_first(self):
        '''
        是否是最后一页

        :Return:
            是最后一页则返回True，否则返回False

        :Example:
         - 例: page.is_last()
        '''
        re_disabled = r'h-page-disabled'
        page_next_li = self._element.find_element_by_attribute(
            'css selector', 'li.h-page-prev[title="上一页"]', timeout=self.timeout)
        if re.search(re_disabled, page_next_li.get_attribute('class')):
            return True
        else:
            return False

    def is_last(self):
        '''
        是否是最后一页

        :Return:
            是最后一页则返回True，否则返回False

        :Example:
         - 例: page.is_last()
        '''
        re_disabled = r'h-page-disabled'
        page_next_li = self._element.find_element_by_attribute(
            'css selector', 'li.h-page-next[title="下一页"]', timeout=self.timeout)
        if re.search(re_disabled, page_next_li.get_attribute('class')):
            return True
        else:
            return False

    def to_page(self, page_number):
        '''
        跳转到指定页

        :Args:
         - page_number: 要跳转的表格页数

        :Example:
         - 例: page.to_page(3)
        '''
        page_input = self._element.find_element_by_attribute(
            'css selector', 'div.h-page-options>div.h-page-options-elevator>input[type="text"]',
            timeout=self.timeout)
        page_input.clear()
        page_input.send_keys(str(page_number))
        page_input.send_keys(Keys.ENTER)

    def get_rows_in_page(self):
        '''
        获取表格页显示的行数

        :Return:
            返回表格页显示的行数

        :Example:
         - 例: page.get_rows_in_page()
        '''
        rows_div = self._element.exist(
            'css selector', 'div.h-page-options>div.h-page-options-sizer',
            timeout=self.timeout)
        if rows_div:
            rows_text = rows_div.text
            rows = int(re.search('\d+', rows_text).group())
            return rows
        else:
            return 0

    def set_rows_in_page(self, rows):
        '''
        设置hui表格单页显示行数

        :Args:
         - rows: 行数

        :Raises:
            SeleniumError: 设置表格行数失败

        :Example:
         - 例: page.set_rows_in_page(10)
        '''
        # 设置表格单页显示行数
        div_select = self._element.find_element_by_attribute(
            'css selector', 'div.h-page-options-sizer div.h-select-single',
            timeout=self.timeout)
        div_option = div_select.find_element_by_attribute(
            'css selector', 'div.h-select-selection', timeout=self.timeout)
        div_option.secure_click()
        ul_selection = div_select.find_element_by_attribute(
            'css selector', 'div.h-select-dropdown div.h-select-dropdown-list',
            timeout=self.timeout)
        lis = ul_selection.find_elements_by_attribute('css selector', 'li.h-select-item',
                                                      timeout=self.timeout)
        for li in lis:
            text = re.findall('(\d+)', li.text)[0]
            if int(text) == rows:
                li.secure_click()
                return
